package com.zzjson.config;

import com.zzjson.scheduler.AnotherBean;
import com.zzjson.scheduler.FirstScheduleJob;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.*;

/**
 * <p>****************************************************************************</p>
 * <p><b>Copyright © 2010-2019 shuncom team All Rights Reserved<b></p>
 * <ul style="margin:15px;">
 * <li>Description : com.zzjson.config</li>
 * <li>Version     : 1.0.0</li>
 * <li>Creation    : 2019年04月10日</li>
 * <li>@author     : zzy0_0</li>
 * </ul>
 * <p>****************************************************************************</p>
 */
@Configuration
public class SchdulerConfiguration {
    @Bean("simpleJobDetail")
    public MethodInvokingJobDetailFactoryBean setMethodInvokingJobDetailFactoryBean(@Qualifier("myBean") Object targetObject,
                                                                                    @Value("printMessage") String targetMethod) {
        MethodInvokingJobDetailFactoryBean methodInvokingJobDetailFactoryBean = new MethodInvokingJobDetailFactoryBean();
        methodInvokingJobDetailFactoryBean.setTargetObject(targetObject);
        methodInvokingJobDetailFactoryBean.setTargetMethod(targetMethod);
        return methodInvokingJobDetailFactoryBean;
    }

    @Bean("firstComplexJobDetail")
    public JobDetailFactoryBean jobDetailFactoryBean(AnotherBean anotherBean) {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setJobClass(FirstScheduleJob.class);
        JobDataMap jobDataMap = new JobDataMap();
        jobDataMap.put("anotherBean", anotherBean);
        jobDetailFactoryBean.setJobDataMap(jobDataMap);

        jobDetailFactoryBean.setDurability(true);
        return jobDetailFactoryBean;
    }

    @Bean
    public SimpleTriggerFactoryBean simpleTriggerFactoryBean(@Qualifier("simpleJobDetail") JobDetail jobDetail,
                                                             @Value("1000") long startDelay,
                                                             @Value("2000") long repeatInterval) {

        SimpleTriggerFactoryBean simpleTriggerFactoryBean = new SimpleTriggerFactoryBean();
        simpleTriggerFactoryBean.setJobDetail(jobDetail);
        simpleTriggerFactoryBean.setStartDelay(startDelay);
        simpleTriggerFactoryBean.setRepeatInterval(repeatInterval);
        return simpleTriggerFactoryBean;
    }


    @Bean
    public CronTriggerFactoryBean cronTriggerFactoryBean(@Qualifier("firstComplexJobDetail") JobDetail jobDetail,
                                                         @Value("0/5 * * ? * *") String cronExpression) {
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        cronTriggerFactoryBean.setCronExpression(cronExpression);
        cronTriggerFactoryBean.setJobDetail(jobDetail);

        return cronTriggerFactoryBean;
    }

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean,
                                                     MethodInvokingJobDetailFactoryBean methodInvokingJobDetailFactoryBean,
                                                     SimpleTriggerFactoryBean simpleTriggerFactoryBean,
                                                     CronTriggerFactoryBean cronTriggerFactoryBean) {

        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        schedulerFactoryBean.setJobDetails(jobDetailFactoryBean.getObject(),
                methodInvokingJobDetailFactoryBean.getObject());
        schedulerFactoryBean.setTriggers(simpleTriggerFactoryBean.getObject(),
                cronTriggerFactoryBean.getObject());
        return schedulerFactoryBean;
    }

}